<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="700px" @close="resetForm">
    <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="80px">
      <el-form-item label="point">
        <el-select v-model="form.point" filterable placeholder="point" style="width: 500px" value-key="uid" clearable>
          <el-option
            v-for="item in pointList"
            :key="item.uid"
            :value="item"
            :label="item.name"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="参数">
        <el-select v-model="form.tag" filterable placeholder="参数名称" style="width: 500px" value-key="uid" clearable>
          <el-option
            v-for="item in tagList"
            :key="item.uid"
            :value="item"
            :label="item.name"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="opc">
        <el-input v-model="form.opc.id" clearable />
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      loading: false, dialog: false, isAdd: true,
      tagList: [],
      pointList: [],
      opcList: [],
      form: {
        'opc': {
          'id': ''
        }
      },
      rules: {}
      // props: {
      //   lazy: true,
      //   lazyLoad (node, resolve) {
      //     const { level } = node
      //     const nodes = this.$parent.service.getStationBuilding("{'target_id' = '" + node.id + "'}")
      //   }
      // }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    async doAdd() {
      const res = await this.$parent.service.addPointOpcRelation(this.form)
      this.loading = false
      if (res.flag) {
        // this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.dialog = false
      } else {
        this.$notify({
          title: '添加失败',
          type: 'error',
          duration: 2500
        })
      }
    },
    doEdit() {
      // edit(this.form).then(res => {
      //   this.resetForm()
      //   this.$notify({
      //     title: '修改成功',
      //     type: 'success',
      //     duration: 2500
      //   })
      //   this.loading = false
      //   this.$parent.init()
      // }).catch(err => {
      //   this.loading = false
      //   console.log(err)
      // })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = { 'opc': { 'id': '' }}
    }
  }
}
</script>

<style scoped>

</style>
